<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="评估日期" prop="assessDate">
        <el-date-picker clearable
          v-model="queryParams.assessDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择评估日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="茶园生态综合评分" prop="totalScore">
        <el-input
          v-model="queryParams.totalScore"
          placeholder="请输入茶园生态综合评分"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="等级名称(优秀/良好/中等/较差)" prop="levelName">
        <el-input
          v-model="queryParams.levelName"
          placeholder="请输入等级名称(优秀/良好/中等/较差)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="较昨日分数变化(正数表示上升,负数表示下降)" prop="compareScore">
        <el-input
          v-model="queryParams.compareScore"
          placeholder="请输入较昨日分数变化(正数表示上升,负数表示下降)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="气候适宜性得分(0-100)" prop="climateScore">
        <el-input
          v-model="queryParams.climateScore"
          placeholder="请输入气候适宜性得分(0-100)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="土壤健康得分(0-100)" prop="soilScore">
        <el-input
          v-model="queryParams.soilScore"
          placeholder="请输入土壤健康得分(0-100)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="水质状况得分(0-100)" prop="waterScore">
        <el-input
          v-model="queryParams.waterScore"
          placeholder="请输入水质状况得分(0-100)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="空气质量得分(0-100)" prop="airScore">
        <el-input
          v-model="queryParams.airScore"
          placeholder="请输入空气质量得分(0-100)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="微生物活性得分(0-100)" prop="microbialScore">
        <el-input
          v-model="queryParams.microbialScore"
          placeholder="请输入微生物活性得分(0-100)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:assessment:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:assessment:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:assessment:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:assessment:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="assessmentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键ID" align="center" prop="id" />
      <el-table-column label="评估日期" align="center" prop="assessDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.assessDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="茶园生态综合评分" align="center" prop="totalScore" />
      <el-table-column label="等级名称(优秀/良好/中等/较差)" align="center" prop="levelName" />
      <el-table-column label="较昨日分数变化(正数表示上升,负数表示下降)" align="center" prop="compareScore" />
      <el-table-column label="气候适宜性得分(0-100)" align="center" prop="climateScore" />
      <el-table-column label="土壤健康得分(0-100)" align="center" prop="soilScore" />
      <el-table-column label="水质状况得分(0-100)" align="center" prop="waterScore" />
      <el-table-column label="空气质量得分(0-100)" align="center" prop="airScore" />
      <el-table-column label="微生物活性得分(0-100)" align="center" prop="microbialScore" />
      <el-table-column label="环境优化建议" align="center" prop="eos" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:assessment:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:assessment:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改环境健康评估主对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="评估日期" prop="assessDate">
          <el-date-picker clearable
            v-model="form.assessDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择评估日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="茶园生态综合评分" prop="totalScore">
          <el-input v-model="form.totalScore" placeholder="请输入茶园生态综合评分" />
        </el-form-item>
        <el-form-item label="等级名称(优秀/良好/中等/较差)" prop="levelName">
          <el-input v-model="form.levelName" placeholder="请输入等级名称(优秀/良好/中等/较差)" />
        </el-form-item>
        <el-form-item label="较昨日分数变化(正数表示上升,负数表示下降)" prop="compareScore">
          <el-input v-model="form.compareScore" placeholder="请输入较昨日分数变化(正数表示上升,负数表示下降)" />
        </el-form-item>
        <el-form-item label="气候适宜性得分(0-100)" prop="climateScore">
          <el-input v-model="form.climateScore" placeholder="请输入气候适宜性得分(0-100)" />
        </el-form-item>
        <el-form-item label="土壤健康得分(0-100)" prop="soilScore">
          <el-input v-model="form.soilScore" placeholder="请输入土壤健康得分(0-100)" />
        </el-form-item>
        <el-form-item label="水质状况得分(0-100)" prop="waterScore">
          <el-input v-model="form.waterScore" placeholder="请输入水质状况得分(0-100)" />
        </el-form-item>
        <el-form-item label="空气质量得分(0-100)" prop="airScore">
          <el-input v-model="form.airScore" placeholder="请输入空气质量得分(0-100)" />
        </el-form-item>
        <el-form-item label="微生物活性得分(0-100)" prop="microbialScore">
          <el-input v-model="form.microbialScore" placeholder="请输入微生物活性得分(0-100)" />
        </el-form-item>
        <el-form-item label="环境优化建议" prop="eos">
          <el-input v-model="form.eos" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAssessment, getAssessment, delAssessment, addAssessment, updateAssessment } from "@/api/system/assessment";

export default {
  name: "Assessment",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 环境健康评估主表格数据
      assessmentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        assessDate: null,
        totalScore: null,
        levelName: null,
        compareScore: null,
        climateScore: null,
        soilScore: null,
        waterScore: null,
        airScore: null,
        microbialScore: null,
        eos: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        assessDate: [
          { required: true, message: "评估日期不能为空", trigger: "blur" }
        ],
        totalScore: [
          { required: true, message: "茶园生态综合评分不能为空", trigger: "blur" }
        ],
        levelName: [
          { required: true, message: "等级名称(优秀/良好/中等/较差)不能为空", trigger: "blur" }
        ],
        compareScore: [
          { required: true, message: "较昨日分数变化(正数表示上升,负数表示下降)不能为空", trigger: "blur" }
        ],
        climateScore: [
          { required: true, message: "气候适宜性得分(0-100)不能为空", trigger: "blur" }
        ],
        soilScore: [
          { required: true, message: "土壤健康得分(0-100)不能为空", trigger: "blur" }
        ],
        waterScore: [
          { required: true, message: "水质状况得分(0-100)不能为空", trigger: "blur" }
        ],
        airScore: [
          { required: true, message: "空气质量得分(0-100)不能为空", trigger: "blur" }
        ],
        microbialScore: [
          { required: true, message: "微生物活性得分(0-100)不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询环境健康评估主列表 */
    getList() {
      this.loading = true;
      listAssessment(this.queryParams).then(response => {
        this.assessmentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        assessDate: null,
        totalScore: null,
        levelName: null,
        compareScore: null,
        climateScore: null,
        soilScore: null,
        waterScore: null,
        airScore: null,
        microbialScore: null,
        eos: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加环境健康评估主";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAssessment(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改环境健康评估主";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAssessment(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAssessment(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除环境健康评估主编号为"' + ids + '"的数据项？').then(function() {
        return delAssessment(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/assessment/export', {
        ...this.queryParams
      }, `assessment_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
